Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 604)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.00335 13.00110 12.99891 12.99677 12.99468 12.99263 12.99062 12.98864
## [9] 12.98669 12.98476 12.98285 12.98095 12.97905 12.97715 12.97525 12.97333
## [17] 12.97140 12.96944 12.96746 12.96544 12.96339 12.96129 12.95914 12.95694
## [25] 12.95467 12.95234 12.94993 12.94745 12.94489 12.94226 12.93957 12.93683
## [33] 12.93405 12.93123 12.92837 12.92548 12.92257 12.91963 12.91669 12.91372
## [41] 12.91076 12.90779 12.90482 12.90187 12.89892 12.89599 12.89309 12.89021
## [49] 12.88736 12.88455 12.88178 12.87906 12.87639 12.87377 12.87121 12.86872
## [57] 12.86630 12.86395 12.86168 12.85949 12.85740 12.85540 12.85349 12.85169
## [65] 12.84987 12.84791 12.84583 12.84363 12.84133 12.83893 12.83646 12.83391
## [73] 12.83130 12.82865 12.82595 12.82324 12.82050 12.81777 12.81503 12.81232
## [81] 12.80964 12.80700 12.80441 12.80188 12.79943 12.79706 12.79478 12.79262
## [89] 12.79057 12.78865 12.78687 12.78525 12.78378 12.78249 12.78139 12.78048
## [97] 12.77978 12.77929 12.77904 12.77862 12.77768 12.77627 12.77442 12.77218
## [105] 12.76961 12.76674 12.76362 12.76031 12.75684 12.75326 12.74961 12.74595
## [113] 12.74232 12.73877 12.73534 12.73207 12.72902 12.72623 12.72375 12.72162
## [121] 12.71988 12.71860 12.71780 12.71754 12.71787 12.71882 12.72045 12.72280
## [129] 12.72646 12.73186 12.73882 12.74715 12.75667 12.76719 12.77854 12.79053
## [137] 12.80297 12.81569 12.82850 12.84121 12.85365 12.86563 12.87696 12.88747
## [145] 12.89696 12.90526 12.91219 12.92026 12.93174 12.94608 12.96272 12.98111
## [153] 13.00069 13.02090 13.04120 13.06102 13.07980 13.09700 13.11206 13.12442
## [161] 13.13352 13.14175 13.15170 13.16318 13.17598 13.18988 13.20468 13.22018
## [169] 13.23615 13.25240 13.26872 13.28490 13.30072 13.31599 13.33050 13.34403
## [177] 13.35638 13.36733 13.37670 13.38425 13.38979 13.39311 13.39550 13.39834
## [185] 13.40152 13.40493 13.40849 13.41209 13.41562 13.41900 13.42211 13.42485
## [193] 13.42714 13.42886 13.42991 13.43020 13.42962 13.42808 13.42547 13.42169
## [201] 13.41664 13.41022 13.40233 13.39287 13.38097 13.36608 13.34858 13.32883
## [209] 13.30723 13.28415 13.25995 13.23503 13.20975 13.18450 13.15964 13.13556
## [217] 13.11264 13.09124 13.07175 13.05113 13.02640 12.99803 12.96647 12.93221
## [225] 12.89569 12.85739 12.81776 12.77728 12.73641 12.69561 12.65534 12.61608
## [233] 12.57828 12.54240 12.50893 12.47831 12.45101 12.42750 12.40522 12.38150
## [241] 12.35658 12.33072 12.30418 12.27722 12.25009 12.22306 12.19637 12.17029
## [249] 12.14507 12.12097 12.09825 12.07716 12.05797 12.04092 12.02623 12.01375
## [257] 12.00318 11.99423 11.98661 11.98004 11.97422 11.96886 11.96369 11.95839
## [265] 11.95269 11.94630 11.93892 11.93027 11.92216 11.91644 11.91285 11.91110
## [273] 11.91093 11.91209 11.91428 11.91726 11.92075 11.92449 11.92820 11.93162
## [281] 11.93448 11.93651 11.93745 11.93703 11.93497 11.93102 11.92490 11.91820
## [289] 11.91258 11.90783 11.90377 11.90023 11.89699 11.89389 11.89073 11.88733
## [297] 11.88349 11.87903 11.87376 11.86749 11.86004 11.85157 11.84245 11.83275
## [305] 11.82256 11.81196 11.80104 11.78988 11.77857 11.76719 11.75583 11.74457
## [313] 11.73350 11.72270 11.71225 11.70225 11.69277 11.68092 11.66429 11.64371
## [321] 11.62000 11.59401 11.56655 11.53845 11.51055 11.48368 11.45865 11.43631
## [329] 11.41748 11.40298 11.39365 11.38631 11.37739 11.36714 11.35584 11.34374
## [337] 11.33110 11.31819 11.30527 11.29260 11.28044 11.26905 11.25869 11.24963
## [345] 11.24212 11.23643 11.23282 11.23155 11.23288 11.23707 11.24404 11.25337
## [353] 11.26484 11.27820 11.29324 11.30971 11.32740 11.34607 11.36548 11.38542
## [361] 11.40564 11.42593 11.44604 11.46575 11.48483 11.50305 11.52271 11.54603
## [369] 11.57267 11.60226 11.63444 11.66886 11.70516 11.74299 11.78198 11.82179
## [377] 11.86204 11.90240 11.94249 11.98197 12.02048 12.05765 12.09314 12.12658
## [385] 12.15762 12.18590 12.21106 12.23705 12.26751 12.30163 12.33861 12.37765
## [393] 12.41795 12.45871 12.49912 12.53838 12.57570 12.61026 12.64127 12.66793
## [401] 12.68943 12.70848 12.72822 12.74849 12.76911 12.78994 12.81079 12.83151
## [409] 12.85193 12.87189 12.89123 12.90977 12.92736 12.94384 12.95903 12.97277
## [417] 12.98490 12.99525 13.00366 13.00997 13.01337 13.01343 13.01057 13.00519
## [425] 12.99770 12.98850 12.97801 12.96664 12.95479 12.94287 12.93129 12.92045
## [433] 12.91078 12.90266 12.89652 12.88945 12.87863 12.86459 12.84784 12.82892
## [441] 12.80834 12.78664 12.76434 12.74197 12.72004 12.69908 12.67963 12.66220
## [449] 12.64731 12.63551 12.62358 12.60835 12.59039 12.57023 12.54844 12.52556
## [457] 12.50215 12.47876 12.45594 12.43425 12.41424 12.39645 12.38145 12.36979
## [465] 12.35954 12.34849 12.33679 12.32454 12.31189 12.29895 12.28586 12.27274
## [473] 12.25971 12.24691 12.23446 12.22249 12.21113 12.20050 12.19073 12.18195
## [481] 12.17428 12.16785 12.16279 12.15862 12.15479 12.15132 12.14823 12.14555
## [489] 12.14328 12.14145 12.14008 12.13919 12.13880 12.13892 12.13958 12.14079
## [497] 12.14258 12.14497 12.14797 12.15240 12.15895 12.16740 12.17756 12.18924
## [505] 12.20224 12.21636 12.23140 12.24717 12.26347 12.28010 12.29687 12.31358
## [513] 12.33003 12.34602 12.36136 12.37585 12.38929 12.40149 12.41224 12.42136
## [521] 12.43185 12.44636 12.46418 12.48457 12.50680 12.53016 12.55390 12.57732
## [529] 12.59968 12.62024 12.63830 12.65312 12.66396 12.67012 12.67378 12.67759
## [537] 12.68149 12.68543 12.68935 12.69320 12.69693 12.70047 12.70378 12.70680
## [545] 12.70948 12.71175 12.71358 12.71489 12.71564 12.71577 12.71522 12.71395
## [553] 12.71190 12.70900 12.70522 12.70049 12.69476 12.68797 12.68006 12.67100
## [561] 12.66129 12.65145 12.64141 12.63110 12.62043 12.60935 12.59776 12.58561
## [569] 12.57281 12.55929 12.54498 12.52980 12.51369 12.49656 12.47864 12.46019
## [577] 12.44120 12.42165 12.40153 12.38082 12.35952 12.33760 12.31506 12.29187
## [585] 12.26803 12.24352 12.21833 12.19244 12.16584 12.13851 12.11028 12.08100
## [593] 12.05072 12.01948 11.98735 11.95436 11.92056 11.88601 11.85074 11.81482
## [601] 11.77828 11.74118 11.70356 11.66548
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 604)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62216 12.61781 12.61357 12.60945 12.60542 12.60150 12.59767 12.59393
## [9] 12.59028 12.58672 12.58324 12.57984 12.57652 12.57326 12.57007 12.56695
## [17] 12.56389 12.56088 12.55793 12.55502 12.55217 12.54935 12.54657 12.54383
## [25] 12.54112 12.53844 12.53578 12.53314 12.53052 12.52791 12.52532 12.52275
## [33] 12.52021 12.51769 12.51521 12.51276 12.51036 12.50800 12.50569 12.50343
## [41] 12.50123 12.49909 12.49701 12.49501 12.49307 12.49122 12.48944 12.48775
## [49] 12.48615 12.48464 12.48323 12.48193 12.48072 12.47963 12.47865 12.47778
## [57] 12.47704 12.47642 12.47594 12.47558 12.47536 12.47529 12.47536 12.47558
## [65] 12.47591 12.47631 12.47679 12.47734 12.47797 12.47869 12.47948 12.48036
## [73] 12.48133 12.48238 12.48353 12.48477 12.48610 12.48754 12.48907 12.49070
## [81] 12.49244 12.49428 12.49623 12.49829 12.50046 12.50275 12.50515 12.50767
## [89] 12.51032 12.51308 12.51597 12.51899 12.52213 12.52541 12.52882 12.53236
## [97] 12.53604 12.53986 12.54382 12.54779 12.55164 12.55537 12.55901 12.56256
## [105] 12.56605 12.56949 12.57288 12.57626 12.57961 12.58298 12.58636 12.58977
## [113] 12.59322 12.59674 12.60032 12.60400 12.60777 12.61167 12.61569 12.61985
## [121] 12.62417 12.62867 12.63335 12.63822 12.64332 12.64864 12.65462 12.66158
## [129] 12.66940 12.67796 12.68711 12.69673 12.70670 12.71688 12.72715 12.73737
## [137] 12.74742 12.75717 12.76648 12.77524 12.78330 12.79055 12.79894 12.81028
## [145] 12.82424 12.84049 12.85868 12.87849 12.89957 12.92159 12.94422 12.96711
## [153] 12.98994 13.01236 13.03404 13.05464 13.07383 13.09127 13.10663 13.11957
## [161] 13.12975 13.13967 13.15185 13.16602 13.18191 13.19923 13.21771 13.23708
## [169] 13.25705 13.27735 13.29770 13.31782 13.33745 13.35629 13.37408 13.39054
## [177] 13.40539 13.41835 13.42915 13.43751 13.44315 13.44580 13.44641 13.44611
## [185] 13.44493 13.44289 13.44003 13.43636 13.43191 13.42670 13.42075 13.41410
## [193] 13.40676 13.39875 13.39011 13.38086 13.37101 13.36060 13.34964 13.33817
## [201] 13.32620 13.31376 13.30088 13.28757 13.27159 13.25106 13.22665 13.19898
## [209] 13.16870 13.13645 13.10287 13.06859 13.03425 13.00050 12.96798 12.93733
## [217] 12.90918 12.88417 12.86295 12.84192 12.81733 12.78961 12.75919 12.72649
## [225] 12.69195 12.65599 12.61903 12.58150 12.54384 12.50646 12.46979 12.43427
## [233] 12.40031 12.36835 12.33881 12.31212 12.28871 12.26899 12.25130 12.23370
## [241] 12.21623 12.19895 12.18191 12.16515 12.14873 12.13269 12.11709 12.10196
## [249] 12.08737 12.07337 12.05999 12.04729 12.03532 12.02413 12.01476 12.00799
## [257] 12.00346 12.00081 11.99970 11.99977 12.00066 12.00203 12.00351 12.00477
## [265] 12.00543 12.00515 12.00357 12.00034 11.99802 11.99911 12.00322 12.00995
## [273] 12.01890 12.02967 12.04185 12.05506 12.06888 12.08293 12.09679 12.11006
## [281] 12.12236 12.13328 12.14241 12.14936 12.15373 12.15512 12.15313 12.14981
## [289] 12.14739 12.14566 12.14445 12.14357 12.14282 12.14202 12.14099 12.13953
## [297] 12.13746 12.13459 12.13073 12.12569 12.11930 12.11087 12.10006 12.08718
## [305] 12.07253 12.05642 12.03915 12.02103 12.00237 11.98347 11.96464 11.94618
## [313] 11.92840 11.91161 11.89610 11.88220 11.87019 11.85655 11.83809 11.81563
## [321] 11.79000 11.76204 11.73255 11.70238 11.67235 11.64329 11.61603 11.59139
## [329] 11.57020 11.55328 11.54148 11.53158 11.52002 11.50707 11.49301 11.47810
## [337] 11.46263 11.44685 11.43105 11.41550 11.40046 11.38621 11.37303 11.36118
## [345] 11.35094 11.34258 11.33637 11.33259 11.33150 11.33338 11.33800 11.34480
## [353] 11.35357 11.36412 11.37624 11.38971 11.40434 11.41991 11.43622 11.45307
## [361] 11.47025 11.48755 11.50476 11.52169 11.53811 11.55384 11.57113 11.59214
## [369] 11.61651 11.64389 11.67391 11.70620 11.74041 11.77618 11.81314 11.85093
## [377] 11.88919 11.92756 11.96568 12.00319 12.03972 12.07491 12.10840 12.13984
## [385] 12.16885 12.19508 12.21816 12.24135 12.26776 12.29681 12.32792 12.36052
## [393] 12.39403 12.42789 12.46150 12.49430 12.52572 12.55517 12.58208 12.60588
## [401] 12.62599 12.64459 12.66414 12.68447 12.70539 12.72672 12.74829 12.76993
## [409] 12.79145 12.81268 12.83343 12.85354 12.87282 12.89110 12.90819 12.92393
## [417] 12.93814 12.95063 12.96122 12.96975 12.97663 12.98241 12.98715 12.99090
## [425] 12.99372 12.99565 12.99675 12.99707 12.99665 12.99556 12.99385 12.99156
## [433] 12.98875 12.98547 12.98177 12.97597 12.96666 12.95429 12.93933 12.92224
## [441] 12.90347 12.88348 12.86273 12.84168 12.82080 12.80053 12.78134 12.76369
## [449] 12.74803 12.73483 12.72056 12.70189 12.67956 12.65429 12.62683 12.59789
## [457] 12.56823 12.53855 12.50961 12.48214 12.45686 12.43451 12.41582 12.40153
## [465] 12.38861 12.37375 12.35722 12.33928 12.32023 12.30033 12.27987 12.25910
## [473] 12.23832 12.21780 12.19780 12.17862 12.16051 12.14377 12.12866 12.11545
## [481] 12.10443 12.09588 12.09005 12.08554 12.08079 12.07592 12.07105 12.06629
## [489] 12.06173 12.05751 12.05373 12.05051 12.04795 12.04616 12.04527 12.04537
## [497] 12.04659 12.04904 12.05282 12.05871 12.06726 12.07822 12.09135 12.10643
## [505] 12.12321 12.14145 12.16093 12.18139 12.20260 12.22433 12.24633 12.26838
## [513] 12.29022 12.31164 12.33237 12.35220 12.37088 12.38818 12.40385 12.41766
## [521] 12.43354 12.45492 12.48084 12.51033 12.54244 12.57620 12.61065 12.64482
## [529] 12.67774 12.70846 12.73601 12.75943 12.77775 12.79001 12.79917 12.80877
## [537] 12.81872 12.82894 12.83934 12.84984 12.86034 12.87076 12.88102 12.89103
## [545] 12.90070 12.90995 12.91868 12.92682 12.93427 12.94096 12.94678 12.95166
## [553] 12.95552 12.95825 12.95979 12.96003 12.95890 12.95630 12.95216 12.94638
## [561] 12.93972 12.93291 12.92588 12.91852 12.91076 12.90248 12.89361 12.88404
## [569] 12.87370 12.86248 12.85030 12.83706 12.82267 12.80704 12.79043 12.77317
## [577] 12.75523 12.73661 12.71729 12.69726 12.67651 12.65502 12.63278 12.60977
## [585] 12.58599 12.56141 12.53603 12.50984 12.48281 12.45493 12.42600 12.39582
## [593] 12.36446 12.33197 12.29842 12.26385 12.22832 12.19189 12.15463 12.11657
## [601] 12.07779 12.03833 11.99826 11.95762
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 604)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.99512 11.99157 11.98813 11.98478 11.98152 11.97834 11.97525 11.97222
## [9] 11.96926 11.96635 11.96350 11.96070 11.95793 11.95519 11.95248 11.94979
## [17] 11.94711 11.94444 11.94177 11.93909 11.93640 11.93369 11.93095 11.92818
## [25] 11.92537 11.92251 11.91961 11.91664 11.91361 11.91051 11.90733 11.90407
## [33] 11.90071 11.89726 11.89370 11.89004 11.88628 11.88244 11.87855 11.87460
## [41] 11.87060 11.86657 11.86250 11.85842 11.85432 11.85021 11.84611 11.84202
## [49] 11.83795 11.83391 11.82991 11.82595 11.82204 11.81819 11.81441 11.81071
## [57] 11.80710 11.80357 11.80016 11.79685 11.79366 11.79059 11.78766 11.78488
## [65] 11.78225 11.77977 11.77747 11.77534 11.77340 11.77164 11.77009 11.76840
## [73] 11.76624 11.76365 11.76066 11.75731 11.75363 11.74967 11.74544 11.74100
## [81] 11.73637 11.73159 11.72670 11.72173 11.71671 11.71168 11.70669 11.70175
## [89] 11.69691 11.69220 11.68766 11.68332 11.67922 11.67540 11.67188 11.66871
## [97] 11.66591 11.66353 11.66160 11.66016 11.65923 11.65886 11.65908 11.65993
## [105] 11.66144 11.66365 11.66629 11.66908 11.67205 11.67519 11.67853 11.68208
## [113] 11.68585 11.68985 11.69410 11.69860 11.70337 11.70843 11.71378 11.71945
## [121] 11.72543 11.73175 11.73841 11.74544 11.75283 11.76062 11.76880 11.77739
## [129] 11.78768 11.80078 11.81639 11.83421 11.85397 11.87536 11.89809 11.92188
## [137] 11.94644 11.97147 11.99667 12.02177 12.04647 12.07047 12.09350 12.11524
## [145] 12.13543 12.15376 12.16994 12.18749 12.20962 12.23557 12.26459 12.29594
## [153] 12.32885 12.36257 12.39636 12.42946 12.46111 12.49056 12.51706 12.53986
## [161] 12.55821 12.57525 12.59449 12.61562 12.63838 12.66249 12.68766 12.71363
## [169] 12.74011 12.76682 12.79350 12.81985 12.84560 12.87047 12.89419 12.91648
## [177] 12.93706 12.95564 12.97196 12.98573 12.99668 13.00453 13.01061 13.01640
## [185] 13.02185 13.02691 13.03153 13.03565 13.03922 13.04218 13.04449 13.04610
## [193] 13.04694 13.04696 13.04612 13.04436 13.04163 13.03787 13.03303 13.02707
## [201] 13.01992 13.01153 13.00185 12.99084 12.97624 12.95634 12.93188 12.90360
## [209] 12.87222 12.83849 12.80314 12.76691 12.73053 12.69473 12.66025 12.62783
## [217] 12.59821 12.57211 12.55027 12.52855 12.50263 12.47301 12.44018 12.40464
## [225] 12.36687 12.32737 12.28663 12.24515 12.20341 12.16192 12.12116 12.08163
## [233] 12.04382 12.00823 11.97534 11.94564 11.91965 11.89784 11.87837 11.85909
## [241] 11.84003 11.82120 11.80262 11.78431 11.76630 11.74861 11.73125 11.71425
## [249] 11.69763 11.68141 11.66561 11.65025 11.63535 11.62093 11.60752 11.59552
## [257] 11.58475 11.57505 11.56623 11.55813 11.55057 11.54339 11.53640 11.52945
## [265] 11.52235 11.51493 11.50703 11.49847 11.49119 11.48702 11.48562 11.48662
## [273] 11.48968 11.49444 11.50055 11.50766 11.51542 11.52347 11.53146 11.53905
## [281] 11.54587 11.55157 11.55581 11.55823 11.55847 11.55620 11.55104 11.54469
## [289] 11.53896 11.53377 11.52898 11.52451 11.52022 11.51603 11.51181 11.50746
## [297] 11.50287 11.49792 11.49252 11.48654 11.47988 11.47115 11.45930 11.44476
## [305] 11.42798 11.40939 11.38941 11.36848 11.34704 11.32552 11.30435 11.28397
## [313] 11.26480 11.24729 11.23186 11.21895 11.20900 11.19821 11.18303 11.16424
## [321] 11.14263 11.11897 11.09405 11.06866 11.04357 11.01956 10.99743 10.97794
## [329] 10.96189 10.95005 10.94321 10.93942 10.93618 10.93354 10.93149 10.93005
## [337] 10.92924 10.92907 10.92955 10.93071 10.93255 10.93509 10.93835 10.94233
## [345] 10.94706 10.95254 10.95880 10.96585 10.97369 10.98236 10.99298 11.00648
## [353] 11.02253 11.04080 11.06095 11.08265 11.10557 11.12939 11.15376 11.17837
## [361] 11.20287 11.22694 11.25024 11.27245 11.29323 11.31225 11.33163 11.35350
## [369] 11.37764 11.40377 11.43166 11.46104 11.49167 11.52330 11.55567 11.58854
## [377] 11.62164 11.65473 11.68756 11.71988 11.75143 11.78195 11.81121 11.83895
## [385] 11.86491 11.88885 11.91051 11.93209 11.95571 11.98097 12.00748 12.03486
## [393] 12.06271 12.09063 12.11824 12.14515 12.17097 12.19530 12.21775 12.23794
## [401] 12.25547 12.27203 12.28949 12.30766 12.32640 12.34554 12.36491 12.38435
## [409] 12.40370 12.42280 12.44148 12.45958 12.47694 12.49339 12.50877 12.52292
## [417] 12.53568 12.54687 12.55635 12.56394 12.56943 12.57287 12.57448 12.57449
## [425] 12.57314 12.57065 12.56726 12.56320 12.55869 12.55397 12.54927 12.54482
## [433] 12.54084 12.53757 12.53524 12.53208 12.52639 12.51850 12.50874 12.49745
## [441] 12.48496 12.47161 12.45773 12.44365 12.42970 12.41623 12.40356 12.39202
## [449] 12.38196 12.37369 12.36514 12.35423 12.34134 12.32683 12.31108 12.29448
## [457] 12.27738 12.26016 12.24320 12.22686 12.21153 12.19758 12.18537 12.17529
## [465] 12.16568 12.15478 12.14275 12.12976 12.11599 12.10159 12.08675 12.07163
## [473] 12.05640 12.04123 12.02629 12.01175 11.99778 11.98456 11.97224 11.96100
## [481] 11.95101 11.94244 11.93546 11.92873 11.92096 11.91235 11.90311 11.89347
## [489] 11.88363 11.87380 11.86419 11.85503 11.84652 11.83887 11.83229 11.82701
## [497] 11.82323 11.82116 11.82101 11.82251 11.82515 11.82887 11.83358 11.83920
## [505] 11.84565 11.85285 11.86072 11.86919 11.87816 11.88755 11.89730 11.90732
## [513] 11.91752 11.92783 11.93816 11.94845 11.95859 11.96853 11.97817 11.98743
## [521] 11.99917 12.01574 12.03632 12.06008 12.08620 12.11384 12.14219 12.17042
## [529] 12.19770 12.22320 12.24611 12.26558 12.28081 12.29096 12.29864 12.30698
## [537] 12.31588 12.32525 12.33501 12.34507 12.35535 12.36575 12.37618 12.38657
## [545] 12.39682 12.40685 12.41657 12.42589 12.43472 12.44298 12.45057 12.45742
## [553] 12.46344 12.46853 12.47262 12.47560 12.47740 12.47793 12.47710 12.47483
## [561] 12.47187 12.46898 12.46607 12.46304 12.45981 12.45627 12.45235 12.44793
## [569] 12.44293 12.43726 12.43083 12.42353 12.41527 12.40598 12.39591 12.38539
## [577] 12.37443 12.36298 12.35105 12.33860 12.32563 12.31212 12.29805 12.28341
## [585] 12.26817 12.25233 12.23585 12.21874 12.20097 12.18252 12.16314 12.14262
## [593] 12.12104 12.09846 12.07495 12.05056 12.02538 11.99945 11.97285 11.94564
## [601] 11.91789 11.88966 11.86103 11.83204
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")